package com.mjh.partners.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mjh.partners.mapper.CommentMapper;
import com.mjh.partners.mapper.ExperienceMapper;
import com.mjh.partners.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;

@RequestMapping("/CommentController")
@Controller
public class CommentController {
  @Autowired
  private CommentMapper commentMapper;
  @Autowired
  private ExperienceMapper experienceMapper;
  @ResponseBody
  @RequestMapping(value="getAllCommentByEid",produces = {"application/json;charset=UTF-8"})
  public String getAllCommentByEid(int eid, HttpServletRequest request, HttpServletResponse response, Model model) throws  Exception{
    List<Comment> Lists=commentMapper.getAllCommentByEid(eid);
    response.setContentType("text/html;charset=utf-8");
    JSONArray json = JSONArray.fromObject(Lists);
    String result=json.toString();
    return result;
  }
  @ResponseBody
  @RequestMapping("addComment")
  public String addComment(int userid,int eid,String cContent) {
    String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    Comment comment = new Comment();
    comment.setUserid(userid);
    comment.setEid(eid);
    comment.setcContent(cContent);
    comment.setcCreatetime(nowTime);
    boolean flag1=commentMapper.addComment(comment);
    boolean flag2=experienceMapper.addCommentOnEx(eid);
    if(flag1&&flag2) {
      return "success";
    }else {
      return "error";
    }
  }
  @ResponseBody
  @RequestMapping("deleteCom")
  public String deleteCom(int eid,int cid) {
    boolean flag1=commentMapper.deleteByCid(cid);
    boolean flag2=experienceMapper.DeCommentByEid(eid);
    if(flag1&&flag2) {
      return "success";
    }else {
      return "error";
    }
  }
}
